package com.ck.company.controller;

import com.ck.company.service.CpJsonDataService;
import com.ck.constant.ResponseConstant;
import com.ck.model.common.JsonData;
import com.ck.model.common.sys.JsonResponse;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * JSON数据
 *
 * @author linqiang
 * @version 2.0
 * @date 2018-09-11 11:50
 */
@Controller
@RequestMapping("/jsonData")
public class CpJsonDataController {
    private static Logger logger = LoggerFactory.getLogger(CpJsonDataController.class);

    @Autowired
    private CpJsonDataService cpJsonDataService;

    /**
     * 根据ID查询json
     *
     * @return JsonResponse
     */
    @RequestMapping(value = "/getById")
    @ResponseBody
    public Object getById(Long jsonId) {
        try {
            JsonData jsonData = cpJsonDataService.selectByPrimaryKey(jsonId);
            if (null != jsonData) {
                return new JsonResponse(ResponseConstant.CODE_SUCCESS, jsonData, "查询JSON成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpJsonDataController getById 查询JSON失败: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "查询JSON失败");
    }

    /**
     * 新增json
     *
     * @param jsonData json
     * @return Object
     */
    @RequestMapping(value = "/save")
    @ResponseBody
    public Object saveJsonData(JsonData jsonData) {
        try {
            if (null == jsonData) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "参数不能为空");
            }
            if (StringUtils.isBlank(jsonData.getJsonContent())) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "JSON内容不能为空");
            }
            if (null == jsonData.getType()) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "JSON类型不能为空");
            }
            jsonData.setAddTime(System.currentTimeMillis());
            Long jsonId = cpJsonDataService.insert(jsonData);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, jsonId, "查询JSON成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpJsonDataController getById 查询JSON失败: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "查询JSON失败");
    }

    /**
     * 新增json
     *
     * @param jsonData json
     * @return Object
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object updateJsonData(JsonData jsonData) {
        try {
            if (null == jsonData || null == jsonData.getJsonId()) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "参数不能为空");
            }
            if (StringUtils.isBlank(jsonData.getJsonContent())) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "JSON内容不能为空");
            }
            if (null == jsonData.getType()) {
                return new JsonResponse(ResponseConstant.CODE_FAIL, "JSON类型不能为空");
            }
            jsonData.setUpdateTime(System.currentTimeMillis());
            cpJsonDataService.updateByPrimaryKey(jsonData);
            return new JsonResponse(ResponseConstant.CODE_SUCCESS, jsonData.getJsonId(), "查询JSON成功");
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("CpJsonDataController getById 查询JSON失败: " + e.getMessage());
        }
        return new JsonResponse(ResponseConstant.CODE_FAIL, "查询JSON失败");
    }
}
